// 数据请求的接口
import axios from "axios"
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
const baseURL = "http://139.196.144.248:8081"
const serve = {
    baseURL,
    userLogin(ruleForm) {
        return axios.post(baseURL + '/user/login/', {
            userName: ruleForm.userName,
            password: ruleForm.password
        })
    },
    userRegister(ruleForm) {
        return axios.post(baseURL + '/user/register/', {
            userName: ruleForm.userName,
            password: ruleForm.password
        })
    },
    getSurveyPage(QID) {
        return axios.post(baseURL + '/ques/load/', {
            QID
        })
        // return axios.get('./data/survey.json')
    },
    getSurveyPageBy64(QID64) {
        return axios.post(baseURL + '/ques/unlock', {
            QID64
        })
    },
    submitSurveyPage(newPage) {
        newPage.Questions.forEach((item, index) => {
            item.order = index
        });
        return axios.post(baseURL + '/ques/create/', newPage)
    },
    modifySurveyPage(modifiedPage) {
        modifiedPage.Questions.forEach((item, index) => {
            item.order = index
        });
        return axios.post(baseURL + '/ques/modify/', modifiedPage)
    },
    submitAnswer(answerSheet) {
        return axios.post(baseURL + "/ans/add/", answerSheet)
    },
    statisticTest(QID) { //获取答卷统计数据
        return axios.post(baseURL + '/ans/get/', {
            QID: QID
        })
    },
    getDescription() {
        return axios.get('./data/descriptions.json');
    },
    getQuestionSheet() {
        return axios.get('./data/source.json');
    },
    // 下面是得到答案的本地测试函数
    getCorrect() {
        return axios.get('./data/correct.json');
    },
    // 下面是得到答案的后端对接测试
    getCorrectTest(QID) {
        return axios.post(baseURL + '/ans/getresult/', {
            QID: QID,
        })
    },
    getETime(QID) {
        return axios.post(baseURL + '/ques/get_ETime/', {
            QID: QID
        })
    },
    // http://127.0.0.1:8000/ques/load/
    // this.$axios
    //     .post("127.0.0.1/user/login/", {
    //       userName: ruleForm.userName,
    //       password: ruleForm.password
    //     })
    //     .then(res => {
    //       if (res.data.success == 1) {
    //         this.$store.state.isLogin=true;
    //         this.$store.state.userName=this.ruleForm.userName;
    //         alert("登录成功");
    //         this.$router.replace("/register");
    //       } else {
    //         console.log("账号或密码错误");
    //         return false;
    //       }
    //     });
    // 获取我的问卷
    getQuestionnaire(userName) {
        return axios.post(baseURL + '/user/list/', {
            userName: userName
        })
    },
    saveOption(op) { //保存问卷设置
        return axios.post(baseURL + '/ques/changesetting/', op)
    },
    // 获取回收站问卷
    getRecycled(userName) {
        return axios.post(baseURL + '/user/collect/', {
            userName: userName
        })
    },
    // 删除问卷
    deleteQuestionnaire(qid) {
        return axios.post(baseURL + '/ques/c_delete/', {
            qid: qid
        })
    },
    // 更改问卷状态
    changeStatus(qid, status) {
        return axios.post(baseURL + '/ques/change/', {
            qid: qid,
            status: status
        })
    },
    getUserIP() {

    },
    copyQuestionnaire(qid) {
        return axios.post(baseURL + '/ques/copy/', {
            QID: qid
        })
    },
    getSurveyAndAnswer(qid) { //返回两个res
        return axios.all([
            this.statisticTest(qid), this.getSurveyPage(qid)
        ]).then(axios.spread(function (answers, survey) {
            return {
                answers,
                survey
            }
        }))
    },
    changeQID(QID) {
        return axios.post(baseURL + '/ques/change_qid/', {
            QID
        })
    }
}
export default serve